﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Caching;
using GetTogether.Studio.WebService;

public partial class WebService_Components_ProxyCase : GetTogether.Studio.Web.UI.Control
{
    public string MethodName;
    public GetTogether.Studio.WebService.ProjectParameter Parameter;
    protected string PrefixCacheKey;
    protected List<string> CacheKeys;
    protected List<string> ProxyCases;
    protected void Page_Load(object sender, EventArgs e)
    {
        PrefixCacheKey = string.Concat("Proxy", "|", CurrentSession.UserCode, "|", Parameter.ProjectName, "|", MethodName, "|");
        Dictionary<string, List<string>> caches = GetCaches();
        if (caches.ContainsKey(MethodName))
        {
            CacheKeys = caches[MethodName];
            rptResult.DataSource = CacheKeys;
            rptResult.DataBind();
        }
        ProxyCases = WebServiceProxyHelper.GetProxyCases(CurrentSession.UserCode, Parameter.ProjectName, MethodName);
        rptProxyCases.DataSource = ProxyCases;
        rptProxyCases.DataBind();
    }

    protected GetTogether.Studio.WebService.ProxyCaseParameter GetProxyParam(string caseName)
    {
        try
        {
            return GetTogether.Utility.SerializationHelper.FromXml<GetTogether.Studio.WebService.ProxyCaseParameter>(GetTogether.Studio.WebService.WebServiceProxyHelper.GetProxyCaseXml(CurrentSession.UserCode, Parameter.ProjectName, MethodName, caseName, "Parameter.txt"));
        }
        catch (Exception ex)
        {
            GetTogether.Studio.Log.Error(ex);
            return null;
        }
    }

    protected string GetRegularExpression(string caseName)
    {
        ProxyCaseParameter p = GetProxyParam(caseName);
        if (p == null) return string.Empty;
        return p.Regular;
    }

    public Dictionary<string, List<string>> GetCaches()
    {
        Cache _cache = HttpRuntime.Cache;
        IDictionaryEnumerator cacheEnum = _cache.GetEnumerator();
        Dictionary<string, List<string>> ret = new Dictionary<string, List<string>>();
        while (cacheEnum.MoveNext())
        {
            string key = cacheEnum.Key.ToString();
            string[] keyInfo = key.Split('|');
            if (key.StartsWith(PrefixCacheKey) && keyInfo.Length > 3)
            {
                string methodName = keyInfo[3];
                if (!ret.ContainsKey(methodName)) ret[methodName] = new List<string>();
                ret[methodName].Add(cacheEnum.Key.ToString());
            }
        }
        return ret;
    }


}